package com.xiaoshou.luceneutil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import org.springframework.stereotype.Controller;

@Controller
public class SearcherFile {

	public static Document searchOne(IndexSearcher searcher, String q) {
		/*
		 * Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_40);
		 * String[] fields = { "questionname","id" }; Occur[] occurs = new
		 * Occur[] { Occur.SHOULD,Occur.SHOULD }; Query query =
		 * MultiFieldQueryParser.parse(Version.LUCENE_40, q, fields,occurs,
		 * analyzer); TopDocs result = searcher.search(query,
		 * searcher.getIndexReader().maxDoc());
		 */
		Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_40);
		String[] fields = { "id" };
		Occur[] occurs = new Occur[] { Occur.MUST };
		Document doc = new Document();
		try {
			Query query = MultiFieldQueryParser.parse(Version.LUCENE_40, q,
					fields, occurs, analyzer);// parse(Version.LUCENE_40, q,
												// fields,occurs,
												// analyzer);//TermQuery
			if (searcher.getIndexReader().maxDoc() <= 0) {

			}

			TopDocs result = searcher.search(query, searcher.getIndexReader()
					.maxDoc());
			ScoreDoc[] hits = result.scoreDocs;
			doc = searcher.doc(hits[0].doc);
			analyzer.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return doc;
	}

	/**
	 * @param searcher
	 * @param q
	 * @param startindex
	 * @param endindex
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	public static List<Document> search(IndexSearcher searcher, String q,
			int startindex, int endindex) throws IOException, ParseException {
		Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_40);
		String[] fields = { "questionname", "id" };
		Occur[] occurs = new Occur[] { Occur.SHOULD, Occur.SHOULD };
		Query query = MultiFieldQueryParser.parse(Version.LUCENE_40, q, fields,
				occurs, analyzer);
		if (searcher.getIndexReader().maxDoc() <= 0) {
			
			return null;
		} else {

			TopDocs result = searcher.search(query, searcher.getIndexReader()
					.maxDoc());
			ScoreDoc[] hits = result.scoreDocs;
			if ((endindex >= hits.length)) {
				endindex = hits.length - 1;
			}
			List<Document> list = new ArrayList<Document>();
			for (int i = startindex; i <= endindex; i++) {
				Document doc = searcher.doc(hits[i].doc);
				list.add(doc);
			}
			System.out.println("搜索list的长度\t→→→→\t" + list.size());
			analyzer.close();
			return list;
		}
	}

	/*
	 * 搜索的总数
	 */
	public static int getsearchSize(IndexSearcher searcher, String q)
			throws ParseException, IOException {
		Analyzer analyzer = new SmartChineseAnalyzer(Version.LUCENE_40);
		String[] fields = { "questionname" };
		Occur[] occurs = new Occur[] { Occur.SHOULD };
		Query query = MultiFieldQueryParser.parse(Version.LUCENE_40, q, fields,
				occurs, analyzer);
		if (searcher.getIndexReader().maxDoc() > 0) {
			TopDocs result = searcher.search(query, searcher.getIndexReader()
					.maxDoc());
			ScoreDoc[] hits = result.scoreDocs;
			analyzer.close();
			return hits.length;
		} else {
			analyzer.close();
			return 0;
		}
	}

}
